package com.xiaoxu.intranetweb.mybatisplusSzappdb.Dic;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.entity.*;
import com.xiaoxu.intranetweb.mybatisplusSzappdb.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class UserDic {
    @Autowired
    private ISysPartService iSysPartService;
    @Autowired
    private ISysRoleService iSysRoleService;
    @Autowired
    private ICCompanysService iCCompanysService;
    @Autowired
    private ISysUserPartConnService iSysUserPartConnService;
    @Autowired
    private ISysUserRoleConnService iSysUserRoleConnService;
    @Autowired
    private ISysUserOrgConnService iSysUserOrgConnService;
    @Autowired
    private ISysUserService iSysUserService;
    @Autowired
    private ISysRoleInfoFunctionService iSysRoleInfoFunctionService;
    @Autowired
    private ISysRoleInfoFunctionWebService iSysRoleInfoFunctionWebService;

    //    区域类型
    public String getPartDic(String userId) {
        String dic = "";
        if (userId == null) {
            return dic;
        }
        QueryWrapper<SysUserPartConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserPartConn sysUserPartConn = iSysUserPartConnService.getOne(queryWrapper);
        if (sysUserPartConn == null) {
            return dic;
        } else {
            dic = iSysPartService.getById(sysUserPartConn.getPartId()).getPartName();
        }
        return dic;
    }

    public String getRoleDic(String userId) {
        String dic = "";
        if (userId == null) {
            return dic;
        }
        QueryWrapper<SysUserRoleConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserRoleConn sysUserRoleConn = iSysUserRoleConnService.getOne(queryWrapper);
        if (sysUserRoleConn == null) {
            return dic;
        } else {
            dic = iSysRoleService.getById(sysUserRoleConn.getRoleId()).getRoleName();
        }
        return dic;
    }

    public String getOrgDic(String userId) {
        String dic = "";
        if (userId == null) {
            return dic;
        }
        QueryWrapper<SysUserOrgConn> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USER_ID", userId);
        SysUserOrgConn sysUserOrgConn = iSysUserOrgConnService.getOne(queryWrapper);
        if (sysUserOrgConn == null) {
            return dic;
        } else {
            dic = iCCompanysService.getById(sysUserOrgConn.getOrgId()).getCpyCo();
        }
        return dic;
    }

    public String getAppNameListDic(String userId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", userId);
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        String appAuthority1 = sysUser.getAppAuthority1();
        String[] routes = String.valueOf(appAuthority1).split("");
        List<String> appNameList = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < routes.length; i++) {
            if (routes[i].equals("1")) {
                list.add(i);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<SysRoleInfoFunction> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("WEB_FUNCTION_NUMBER",list.get(i).toString());
            SysRoleInfoFunction sysRoleInfoFunction = iSysRoleInfoFunctionService.getOne(queryWrapper2);
            if (sysRoleInfoFunction!=null){
                appNameList.add(sysRoleInfoFunction.getWebFunctionExplain());
            }
        }
        return appNameList.toString();
    }


    public String getWebNameListDic(String userId) {
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ID", userId);
        SysUser sysUser = iSysUserService.getOne(queryWrapper);
        String appAuthority1 = sysUser.getAppAuthority1();
        String[] routes = String.valueOf(appAuthority1).split("");
        List<String> appNameList = new ArrayList<>();
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < routes.length; i++) {
            if (routes[i].equals("1")) {
                list.add(i);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            QueryWrapper<SysRoleInfoFunctionWeb> queryWrapper2=new QueryWrapper<>();
            queryWrapper2.eq("WEB_FUNCTION_NUMBER",list.get(i).toString());
            SysRoleInfoFunctionWeb sysRoleInfoFunctionWeb = iSysRoleInfoFunctionWebService.getOne(queryWrapper2);
            if (sysRoleInfoFunctionWeb!=null){
                appNameList.add(sysRoleInfoFunctionWeb.getWebFunctionExplain());
            }
        }
        return appNameList.toString();
    }
}
